<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="../static/css/styleoneadd.css">
<link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap.min.css">

<style>
    body{
        background-color: #f2f2f2 !important;
    }
</style>
    <!-- 内容主体区域 -->
<div class="layui-fluid-pad" ng-app="question">
    <div class="layui-col-md12" ng-controller="questionManage" id="questionManage">
        <div class="layui-card">
            <div class="layui-card-header">题库管理</div>
            <div class="layui-card-body">

                <div class="layui-row layui-pad15">
                    <form class="layui-form" action="" lay-filter="test_form">
                        <div class="layui-col-md12 layui-col-lh">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md2"><input type="text" id="questionName" name="title"  placeholder="题目名称" class="form-control"/></div>
                                <div class="layui-col-md2">
                                    <select name="quiz1" id="classType" class="form-control" lay-ignore>
                                        <option value="">类型</option>
                                        <option value="{{type.typeName}}" ng-repeat="type in classTypeList">{{type.typeName}}</option>
                                    </select>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-row">
                                        <div class="layui-col-md12 layui-order-flexs">
                                            <input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test1">
                                            &nbsp;&nbsp;至&nbsp;&nbsp;
                                            <input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test2">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md1">
                                    <button type="button" class="layui-btn  layui-t-h" ng-click="screen()">筛选</button>
                                </div>
                                <div class="layui-col-md4 layui-text-right">
                                    <a href="createQuestion.html" class="layui-btn layui-t-h ">
                                        <i class="layui-icon">&#xe6b2;</i>
                                        新增问题
                                    </a>
                                </div>
                            </div>
                        </div>
                    </form>

                    <!--表格-->
                    <div class="layui-row">
                        <div class="layui-col-md12">
                            <table class="layui-data-zs" id="test3" border="1" style="    border: 1px solid #e5e5e5;">
                                <thead>
                                	<tr class="layui-t-h layui-hg-b" style=" background-color: #34C0E3;color: #ffffff;">
	                                    <th>序号</th>
	                                    <th>问题</th>
	                                    <th>类型</th>
	                                    <th>创建时间</th>
	                                    <th>操作</th>
	                                </tr>
                                </thead>
                                <tbody>
                                	<tr class="layui-hg-b" ng-repeat="question in questionList">
                                        <td><span ng-bind="$index + 1"></span></td>
                                        <td><span ng-bind="question.content || '--'"></span></td>
                                        <td><span ng-bind="question.classType || '--'"></span></td>
                                        <td><span ng-bind="(question.createdTime|date:'yyyy-MM-dd') || '--'"></span></td>
                                        <td>
	                                        <a href="#" class="layui-btn-ty layui-zdy-bj" ng-click="modifyQuestion(question.questionsId)">编辑</a>
	                                        <a href="#" class="layui-btn-ty  layui-dels" ng-click="dltQuestion(question.questionsId)">删除</a>
	                                    </td>
	                                </tr>
                                </tbody>
                            </table>
                            <div ng-show="!questionList || questionList.length == 0" style="margin: 20px 0; text-align: center;">无符合条件数据。</div>
                        </div>
                        <!--分页-->
                        <div class="layui-col-md12 layui-page-top" ng-hide="!questionList || questionList.length == 0">
                            <div id="demo7"></div>
                        </div>
                        <!--分页end-->
                    </div>
                    <!--表格end-->



                </div>
            </div>
        </div>
    </div>
</div>

<script src="../static/js/jquery.min.js"></script>
<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/js/angular.js"></script>
<script src="../static/js/app/consts/consts.js"></script>
<script>
//function  deleData(obj) {
//    var data = {
//        token:"delete question",
//        questionId:$(obj).attr('data-id')
//    }
//    console.log(data);
//    $.post('dltQuestion',data,function (result) {
//        console.log(result);
//    },'json')
//
//}

    var getAllQuestionURL = baseUrl + "/exam/getAllQuestion";
    var getAllClassTypeURL = baseUrl + "/exam/getAllClassType";
    var dltQuestionURL = baseUrl +"/exam/dltQuestion";
    var getPartQuestionURL = baseUrl + "/exam/getPartQuestion"

	var app = angular.module('question',['ng']);
    app.controller('questionManage',function($scope, $http){

        var pageConut = 0;
        var pageSize = 10;
        var pageNum = 1;
        var currentQuestion = [];
//        var currentPageNumber = 1;

    	//页面加载时执行的函数
		$http({
			method: 'GET',
			params:{
			    token:"11111",
                pageNumber: pageNum,
                pageSize: pageSize,
                sort: "",
                desc: ""
            },
			url: getAllQuestionURL
		}).then(function successCallback(result){
		    console.log(result.data);
		    console.log(result.data.data);
//			$scope.questionList = result.data.data;
			if(result.data) {
                var resultInfo = result.data.data;
                updateUnitermPagination(resultInfo);
                if(resultInfo) {
                    $scope.questionList = resultInfo.data;
                    console.log($scope.questionList);
                }
//                if(result.data.msg) {
//                    layer.msg(result.data.msg)
//                }
            }
		},function errorCallback(result) {
            console.log("error")
        });
		$http({
			method: 'GET',
			params:{"token":"11111"},
			url: getAllClassTypeURL
		}).then(function successCallback(result){
			$scope.classTypeList = result.data.data;
			console.log($scope.classTypeList);
		});

		//编辑按钮
        $scope.modifyQuestion = function (questionId) {
            console.log(questionId);
            var url = "modifyQuestion.html?questionId=" + questionId;
            window.location.href = url;
        }
		
		//删除按钮
        $scope.dltQuestion = function(questionId) {
            console.log(questionId);
            layer.open({
                area: '300px;',
                title: '确认',
                content: '确认删除？',
                btn: ['确认', '取消'],
                closeBtn: false,
                resize: false,
                yes: function (index, layero) {
                    $http({
                        method:"POST",
                        url:dltQuestionURL,
                        data:{
                            token:"delete question",
                            questionId:questionId
                        },
                        headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                        transformRequest: function(obj) {
                            var str = [];
                            for(var p in obj){
                                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                            }
                            return str.join("&");
                        }
                    }).then(function successCallback(result) {
                        console.log(result.data.message);
                        $scope.screen(pageNum);
                        alertMsg(result.data.message);
                    });
                }
            });
        }

		//筛选按钮
        $scope.screen = function (goPageNumber) {
            if(!goPageNumber) {
                pageNum = 1;
            } else {
                pageNum = goPageNumber;
            }
            var data = {
                token:"",
                content:$('#questionName').val()?($('#questionName').val()):"",
                type:$('#classType').val()?($('#classType').val()):"",
                startTime:$('#test1').val()?($('#test1').val()):"",
                endTime:$('#test2').val()?($('#test2').val()):"",
                pageNumber: pageNum,
                pageSize: pageSize,
                sort: "",
                desc: ""
            }
            $http({
                method:"GET",
                params:data,
                url:getPartQuestionURL
            }).then(function successCallback(result) {
                if(result.data) {
                    var resultInfo = result.data.data;
                    updateUnitermPagination(resultInfo);
                    if(resultInfo) {
                        $scope.questionList = resultInfo.data;
                    }
                }
            },function errorCallback(result) {
                console.log("error")
            });
        }

    });

    function alertMsg(msg) {
        layui.use(['layer'], function () {
            var layer = layui.layer;
            layer.msg(msg);
        });
    }

    function updateUnitermPagination(resultInfo) {
        pageConut = resultInfo.total || 0;
        pageSize = resultInfo.pageSize || 10;
        pageNum = resultInfo.pageNumber || 1;
        currentQuestion = resultInfo;
        refreshPagination();
    }

    // 分页刷新
    function refreshPagination() {
        layui.use(['laypage', 'layer'], function() {
            var laypage = layui.laypage;
            //分页
            laypage.render({
                elem: 'demo7'
                ,count: pageConut
                ,curr: pageNum
                ,limit: pageSize
                ,theme: '#34C0E3'
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                ,jump: function(obj, first) {
                    if (!first) {
                        pageNum = obj.curr;
                        pageSize = obj.limit;
                        var scope = getAngularScope();
                        scope.screen(pageNum);
                    }
                }
            });
        });
    }

    // 获取angular scope
    function getAngularScope() {
        return angular.element(questionManage).scope();
    }

    //Demo
    layui.use(['form','laypage','laydate','table','layer'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var laypage = layui.laypage;
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
     // 日期
        laydate.render({
            elem: '#test1' //指定元素
            ,theme: '#34C0E3'
        });
        laydate.render({
            elem: '#test2' //指定元素
            ,theme: '#34C0E3'
        });
        //分页
//        laypage.render({
//            elem: 'demo7'
//            ,count: 100
//            ,theme: '#34C0E3'
//            ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
//            ,jump: function(obj){
//                // console.log(obj)
//            }
//        });
        //创建单项
        $(function () {
            $('.creatdx').on('click',function () {
                var strU = 'createQuestion.html';
                layer.open({
                    type: 2,
                    title:'创建问题类型',
                    area: ['480px', '500'],
                    shadeClose: true, //开启遮罩关闭
                    content: strU
                });
            });
            form.render('select', 'test_form');
        });
    });
</script>




